<template>
  <div class="my-order-con">
    <div class="navbar">
      <div class="back" @click="goBack"></div>
      <div class="nav-title">我的订单</div>
    </div>
    <div class="empty-con" v-if="isEmpry">
      <div class="empty-text">暂无订单</div>
    </div>
    <div v-else>
      <div class="block" v-for="(item, index) in reverseList" :key="index">
        <div @click="toGoodsDetail(item)">
          <div class="pay-success">{{ item.status | stateFormat }}</div>
          <div class="goods-info-top">
            <div class="img-con">
              <img :src="circleUrl + item.img" width="100px" />
            </div>
            <div class="right-con">
              <div class="detail-con">
                <div class="goods-title">
                  <h1>{{ item.title }}</h1>
                </div>
                <div class="goods-price">
                  <div>￥{{ item.sell_price }}</div>
                  <div style="text-align: right; color: gray">
                    x{{ item.number }}
                  </div>
                </div>
              </div>
              <div style="color: #ffd116">24小时内发货</div>
              <div class="total-price">
                <span style="color: #ccc">
                  总价￥{{ item.number * item.sell_price }}
                </span>
                <span style="color: #ccc" v-if="item.discount">
                  &nbsp;优惠￥{{ item.discount }}
                </span>
                <span v-if="item.discount" style="font-weight: 600">
                  &nbsp;实付款￥{{
                    item.number * item.sell_price - item.discount
                  }}
                </span>
                <span v-else style="font-weight: 600">
                  &nbsp;实付款￥{{ item.number * item.sell_price }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <div class="operate">
          <div @click="delMyOrder(item.id, index)">删除订单</div>
          <div class="comment" @click="toComment(item.goods_id)">评价</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      circleUrl: "http://localhost:9528/admin/news_info/downloadPic?img_url=",
      username: "",
      myOrderList: [],
      isEmpry: false,
    };
  },
  created() {
    if (localStorage.getItem("UserInfo")) {
      this.username = window.JSON.parse(
        localStorage.getItem("UserInfo")
      ).username;
    }
    this.getOrder();
  },
  filters: {
    stateFormat(value) {
      if (value == 0) return "交易关闭";
      else if (value == 1) return "交易成功";
    },
  },
  computed: {
    reverseList() {
      return this.myOrderList.reverse();
    },
    ...mapGetters(["discountStr"]),
  },
  methods: {
    goBack() {
      this.$router.push("/idssp/member");
    },
    // 获得订单
    getOrder() {
      this.$http
        .get("/getMyOrder", {
          params: {
            username: this.username,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            let data = res.data.message;
            this.myOrderList = data;
            if (this.myOrderList.length > 0) {
              this.isEmpry = false;
            } else {
              this.isEmpry = true;
            }
          } else {
            this.myOrderList = [];
            this.isEmpry = true;
          }
        });
    },
    toGoodsDetail(item) {
      // 请求得到商品id
      this.$http
        .get("/getGoodsByOrder", {
          params: {
            goods_no: item.goods_no,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            let goodsId = 0;
            if (res.data.message[0]) {
              goodsId = res.data.message[0].id;
            }
            // 根据商品货号，跳转到商品详情
            this.$router.push({
              path: "/idssp/shops/goodsinfo/" + goodsId,
            });
          } else {
            Toast("操作失败！！！");
          }
        });
    },
    // 删除我的订单
    delMyOrder(id) {
      this.$messagebox
        .confirm("", {
          message: "是否删除该订单？",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        })
        .then((action) => {
          if (action == "confirm") {
            //确认的回调
            let params = {
              id: id,
            };
            this.$http.post("/delMyOrder", params).then((res) => {
              if (res.data.errcode === 200) {
                Toast("删除成功!!!");
                this.getOrder();
              } else {
                Toast("删除失败!!!");
              }
            });
          }
        })
        .catch((err) => {
          if (err == "cancel") {
            //取消的回调
          }
        });
    },
    toComment(goods_id) {
      this.$router.push({
        name: "goodscmt",
        params: { id: goods_id },
      });
    },
  },
};
</script>
<style lang='scss' scoped>
.my-order-con {
  font-size: 13px;
  background-color: #eee;
  overflow: hidden;
  padding: 0 10px;
  padding-top: 50px; //如果不写则页面会显示不完全
  h1 {
    font-size: 13px;
  }
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    width: 100%;
    height: 44px;
    background-color: #e25b45;
    text-align: center;
    color: white;
    font-size: 16px;
    align-items: center;
    .back {
      width: 50px;
      height: 100%;
      position: absolute;
      left: 20px;
      background: url("../../assets/img/back.png") no-repeat;
      background-size: 40% 40%;
      background-position-y: center;
    }
    .nav-title {
      margin: 0 auto;
    }
  }
  .empty-con {
    width: 200px;
    height: 200px;
    background: url("../../assets/img/order/empty.png") no-repeat;
    background-size: 100% 100%;
    background-position-y: center;
    margin: 0 auto;
    text-align: center;
    .empty-text {
      padding-top: 180px;
      font-size: 15px;
    }
  }
  .block {
    margin-bottom: 10px;
    padding: 5px 10px;
    background-color: #fff;
    .pay-success {
      text-align: right;
      color: orange;
    }
    .goods-info-top {
      width: 100%;
      justify-content: space-between;
      display: flex;
      .right-con {
        .detail-con {
          width: 220px;
          margin-top: 5px;
          display: flex;
          .goods-title {
            width: 190px;
            height: 40px;
          }
        }
      }
    }
    .operate {
      margin-top: 10px;
      display: flex;
      justify-content: space-around;
      div {
        border: 1px solid #ccc;
        padding: 5px;
        border-radius: 20px;
        margin-right: 10px;
        width: 80px;
        height: 30px;
        text-align: center;
      }
      .comment {
        border: 1px solid orange;
        color: orange;
      }
    }
  }
}
</style>